package com.faiz.example;

import java.util.HashMap;
import java.util.Map;

public class Solution {

    public int solution1(String s) {

        if (s.isEmpty() || s == null) {
            return 0;
        }
        //为了节省空间，可以不适用string.toCharArray
        Map<Character, Integer> map = new HashMap<>();
        //记录结果
        int result = 0;
        int i = -1;
        for (int j = 0; j < s.length(); j++) {
            if (map.containsKey(s.charAt(j))) {
                i = Math.max(i, map.get(s.charAt(j)));
            }
            if (j - i > result) {
                result = j - i;
            }
            map.put(s.charAt(j), j);
        }
        return result;
    }
}
